package com.jneagle.xlstool.bzwgjmx.view.gui;

import com.dwarfeng.dutil.basic.cna.model.ReferenceModel;
import com.dwarfeng.dutil.basic.cna.model.obs.ReferenceAdapter;
import com.dwarfeng.dutil.basic.gui.swing.SwingUtil;
import com.jneagle.xlstool.bzwgjmx.view.handler.I18nHandler;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.io.File;
import java.util.List;

/**
 * 原始文件查看对话框。
 *
 * @author DwArFeng
 * @since 1.0.0
 */
@Component
@DependsOn("viewConfiguration")
public class RawFileInspectDialog extends JDialog {

    private static final long serialVersionUID = 7294350194724249347L;

    private final ApplicationContext ctx;

    private final I18nHandler i18nHandler;

    private final ReferenceModel<List<File>> rawFileModel;

    private final JTable fileTable = new JTable();

    private final FileTableModel fileTableModel = new FileTableModel();

    private final RawFileObserver observer = new RawFileObserver();

    public RawFileInspectDialog(
            ApplicationContext ctx,
            I18nHandler i18nHandler,
            ReferenceModel<List<File>> rawFileModel
    ) {
        this.ctx = ctx;
        this.i18nHandler = i18nHandler;
        this.rawFileModel = rawFileModel;
    }

    @SuppressWarnings("DuplicatedCode")
    @PostConstruct
    public void postConstruct() {
        // 初始化窗口。
        setTitle(i18nHandler.getMessage("ui.frame.002"));
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setBounds(100, 100, 600, 450);
        setModal(true);
        // 初始化 contentPanel。
        JPanel contentPanel = new JPanel();
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPanel);
        contentPanel.setLayout(new BorderLayout(0, 0));
        // 向 contentPanel 中添加表格组件。
        I18nTableCellRenderer structuredImportErrorInfoHeaderRender = ctx.getBean(
                I18nTableCellRenderer.class,
                ctx.getBean(I18nHandler.class),
                fileTable.getTableHeader().getDefaultRenderer()
        );
        fileTable.getTableHeader().setDefaultRenderer(structuredImportErrorInfoHeaderRender);
        fileTable.setModel(fileTableModel);
        fileTable.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
        TableColumn resizingColumn = fileTable.getColumnModel().getColumn(0);
        fileTable.getTableHeader().setResizingColumn(resizingColumn);
        TableColumn i18nColumn = fileTable.getColumnModel().getColumn(1);
        resizingColumn.setPreferredWidth(80);
        JScrollPane structuredImportErrorInfoScrollPane = new JScrollPane();
        structuredImportErrorInfoScrollPane.setViewportView(fileTable);
        add(structuredImportErrorInfoScrollPane, BorderLayout.CENTER);

        // 添加侦听器。
        rawFileModel.addObserver(observer);

        // 同步属性。
        syncProperties();
    }

    private void syncProperties() {
        SwingUtil.invokeInEventQueue(() -> fileTableModel.updateData(rawFileModel.get()));
    }

    @PreDestroy
    public void preDestroy() {
        // 移除侦听器。
        rawFileModel.removeObserver(observer);
    }

    private class RawFileObserver extends ReferenceAdapter<List<File>> {

        @Override
        public void fireSet(List<File> oldValue, List<File> newValue) {
            SwingUtil.invokeInEventQueue(() -> fileTableModel.updateData(newValue));
        }
    }
}
